package com.gitee.sop.layui.adminserver.module.background.admin.manager;

import com.gitee.sop.layui.adminserver.module.background.admin.entity.SysApplication;
import com.gitee.sop.layui.adminserver.module.background.admin.mapper.SysApplicationMapper;
import com.gitee.sop.layui.adminserver.plugins.cache.util.CacheUtil;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @author: huwei
 * @date: 2020/4/24 13:49
 * @version: 1.0.0
 */
@Component
public class SysApplicationManager {
    @Lazy
    @Resource
    private SysApplicationMapper sysApplicationMapper;

    public void putDisable(String appId) {
        CacheUtil.put(appId, 1, this.getClass().getName());
    }

    public boolean isDisable(String appId) {
        return CacheUtil.get(appId, this.getClass().getName()) != null;
    }

    public void delDisable(String appId) {
        CacheUtil.remove(appId, this.getClass().getName());
    }

    public List<SysApplication> getRelationListByUserUuid(String userUuid) {
        return CacheUtil.getOrPutIfAbsent(userUuid, () -> this.sysApplicationMapper.selectListByRelationUser(userUuid), this.getClass().getName());
    }

    public SysApplication getOneByAppId(String appId) {
        return CacheUtil.getOrPutIfAbsent(appId, () -> this.sysApplicationMapper.selectOneByAppId(appId), this.getClass().getName());
    }

    public SysApplication getOneByAppIdAndCreatorUuid(String appId, String uuid) {
        return CacheUtil.getOrPutIfAbsent(appId + ":" + uuid, () -> this.sysApplicationMapper.selectOneByAppIdAndCreatorUuid(appId, uuid), this.getClass().getName());
    }


    public List<SysApplication> getListInAppId(List<String> appIdList) {
        return CacheUtil.getOrPutIfAbsent(Arrays.toString(appIdList.toArray()), () -> this.sysApplicationMapper.selectListInAppId(appIdList), this.getClass().getName());
    }

    public void clearCache() {
        CacheUtil.clearDomain(this.getClass().getName());
    }
}
